package com.hwadee.job.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hwadee.job.entity.Company;
import com.hwadee.job.entity.User;
import com.hwadee.job.service.CompanyService;
import com.hwadee.job.service.UserService;

@RequestMapping("/company")
@Controller
public class CompanyController {

	@Autowired
	private CompanyService companyService;
	@Autowired
	private UserService userService;
	
	@GetMapping("/index")
	public String index(Model model,HttpServletRequest request,HttpSession session) {
		User user=(User) session.getAttribute("user");
		Company com=companyService.selectByName(user.getuName());
		model.addAttribute("company", com);
		return "company/company";
	}
	
	@PostMapping("/companylogin")
	public String comRecruit(User user, Model model, HttpSession session,HttpServletRequest request) {
		String username = user.getuName();
		String userpassword = user.getuPassword();
		if (username == "" || username.equals(null) || userpassword == "" || userpassword.equals(null)) {
			model.addAttribute("msg", "账号或密码不能为空！");
			return "company/company";
		}
		User use = userService.selectByName(username);
//		User use=(User) session.getAttribute("user");
		if (userpassword.equals(use.getuPassword()) && username.equals(use.getuName())) {
			session.setAttribute("user", use);
			return "company/intro";
		}else {
			model.addAttribute("msg", "账号或密码错误！");
			return "company/company";
		}
		
	}
	
	@GetMapping("/exit")
	public String exit(Model model, HttpSession session) {
		User user=(User) session.getAttribute("user");
		if(user.getuType().equalsIgnoreCase("student")) {
			 return "redirect:/user/index";
		}else {
			return "redirect:/user/login";
		}
	}
}
